package com.ruoyi.game.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.game.domain.GameRole;
import com.ruoyi.game.domain.dto.GameRoleAddDTO;
import com.ruoyi.game.domain.dto.GameRolePageDTO;
import com.ruoyi.game.domain.dto.GameRoleUpdateDTO;
import com.ruoyi.game.mapper.GameRoleMapper;
import com.ruoyi.game.mapper.GameXinfaMapper;
import com.ruoyi.game.service.IGameRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 角色职业Service业务层处理
 *
 * @author ghostmen
 * @date 2024-10-29
 */
@Service
public class GameRoleServiceImpl extends ServiceImpl<GameRoleMapper, GameRole> implements IGameRoleService {

    @Autowired
    private GameRoleMapper gameRoleMapper;
    @Autowired
    private GameXinfaMapper gameXinfaMapper;

    /**
     * 查询角色职业
     *
     * @param roleId 角色职业主键
     * @return 角色职业
     */
    @Override
    public GameRole selectGameRoleByRoleId(Long roleId) {
        return gameRoleMapper.selectGameRoleByRoleId(roleId);
    }

    /**
     * 查询角色职业列表
     *
     * @param pageDTO 角色职业
     * @return 角色职业
     */
    @Override
    public List<GameRole> selectGameRoleList(GameRolePageDTO pageDTO) {
//        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return gameRoleMapper.selectGameRoleList(pageDTO);
    }

    @Override
    public List<GameRole> getGameRoleList() {
        return gameRoleMapper.getGameRoleList();
    }

    /**
     * 新增角色职业
     *
     * @param gameRoleAddDTO 角色职业
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertGameRole(GameRoleAddDTO gameRoleAddDTO) {
        //判断是否存在其他角色
        Long count = gameRoleMapper.selectCount(new QueryWrapper<GameRole>().lambda()
                .eq(GameRole::getRoleName, gameRoleAddDTO.getRoleName()));
        if (count > 0) {
            throw new ServiceException("该角色《" + gameRoleAddDTO.getRoleName() + "》已存在，请勿重复添加");
        }
        Date nowDate = DateUtils.getNowDate();
        GameRole gameRole = BeanUtil.copyProperties(gameRoleAddDTO, GameRole.class);
        gameRole.setRoleId(IdWorker.getId());
        gameRole.setCreateTime(nowDate);
        gameRole.setCreateBy(gameRoleAddDTO.getLoginUser().getUsername());
        gameRole.setDelFlag(Boolean.FALSE);
        int i = gameRoleMapper.insertGameRole(gameRole);
        if (i > 0) {
//            List<GameXinfaAddDTO> gameXinfaList = gameRoleAddDTO.getGameXinfaList();
//            List<GameXinfa> copyToList = BeanUtil.copyToList(gameXinfaList, GameXinfa.class);
//            copyToList.forEach(gameXinfa -> {
//                gameXinfa.setRoleId(gameRole.getRoleId());
//                gameXinfa.setCreateBy(gameRoleAddDTO.getLoginUser().getUsername());
//                gameXinfa.setCreateTime(nowDate);
//                gameXinfa.setDelFlag(Boolean.FALSE);
//            });
//            gameXinfaMapper.batchInsertGameXinfa(copyToList);
        }
        return i;
    }


    /**
     * 修改角色职业
     *
     * @param gameRoleUpdateDTO 角色职业
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateGameRole(GameRoleUpdateDTO gameRoleUpdateDTO) {
        Long count = gameRoleMapper.selectCount(new QueryWrapper<GameRole>().lambda()
                .eq(GameRole::getRoleName, gameRoleUpdateDTO.getRoleName())
                .ne(GameRole::getRoleId, gameRoleUpdateDTO.getRoleId())
        );
        if (count > 0) {
            throw new ServiceException("该角色《" + gameRoleUpdateDTO.getRoleName() + "》已存在，请勿重复添加");
        }
        GameRole gameRole = BeanUtil.copyProperties(gameRoleUpdateDTO, GameRole.class);
        gameRole.setUpdateTime(DateUtils.getNowDate());
        gameRole.setUpdateBy(gameRoleUpdateDTO.getLoginUser().getUsername());
        return gameRoleMapper.updateGameRole(gameRole);
    }

    /**
     * 批量删除角色职业
     *
     * @param roleIds 需要删除的角色职业主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchDeleteGameRoleByRoleIds(Long[] roleIds) {
        return gameRoleMapper.batchDeleteGameRoleByRoleIds(roleIds);
    }

    /**
     * 删除角色职业信息
     *
     * @param roleId 角色职业主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteGameRoleByRoleId(Long roleId) {
        return gameRoleMapper.deleteGameRoleByRoleId(roleId);
    }
}
